package com.tuling.mybaitis_spring.proxy;

import com.tuling.User;
import com.tuling.mybaitis_spring.service.UserService;
import org.springframework.cglib.proxy.*;

import java.lang.reflect.Method;

public class ProxyEnhancer {
	public static void main(String[] args) {

		UserService target = new UserService();
		Enhancer enhancer = new Enhancer();
		enhancer.setSuperclass(UserService.class);
//		enhancer.setCallbacks(new Callback[]{new MethodInterceptor() {
//			@Override
//			public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
//				System.out.println("------------before");
//				Object result = methodProxy.invoke(target, objects);
//				System.out.println("------------after");
//				return result;
//			}}
//		});
		enhancer.setCallbacks(new Callback[]{new MethodInterceptor() {
			@Override
			public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
				//o 是代理对象 target 是被代理对象 method 是原始方法 objects 是原始方法参数  methodProxy 是代理方法
				System.out.println("------------before");
				Object result = methodProxy.invoke(target, objects);
				System.out.println("------------after");
				return result;
			}
		},NoOp.INSTANCE} );

		enhancer.setCallbackFilter(new CallbackFilter(){

			@Override
			public int accept(Method method) {
				return 0;
			}
		});
		System.out.println("ttttttttt");
		UserService userService = (UserService)enhancer.create();
		userService.selectUser("xiahuahua");


	}
}
